const _ = require('lodash');
shoppingCartController.$inject = ['$scope', 'shoppingCartService', '$location'];

function shoppingCartController($scope, shoppingCartService, $location){

    let sc = this;
    $scope.num = 1;
    let merchantId = sessionStorage.getItem("merchantId");    

    sc.message = {};
    // $scope.foodItems = JSON.parse(sessionStorage.getItem())
    // 下订单那里

    sc.addOrderItemToShoppingCart = function addOrderItemToShoppingCart(food){
        let sh = sessionStorage.getItem("shoppingCart");
        let shoppingCart = JSON.parse(sh);
        
        shoppingCartService.addOrderItemToShoppingCart(food, shoppingCart, (err, result)=>{
            sc.message=food.name;
            console.log("返回来的result"+result.length);
            _.forEach(result,(item)=>{
               console.log("加入购物车中的信息为"+ item._id +" "+item.price +" "+item.num +" "+item.subTotalMoney);
            //   / console.log("data "+key+"____"+result[key].price);
           });
            sessionStorage.setItem('shoppingCart', JSON.stringify(result));
           
            // sessionStorage.shoppingCart = result;
        });

    }

    sc.plusOneItem = function plusOneItem(food){
        let customer = sessionStorage.getItem('customer');
        console.log(customer);
        food.num++;
    }

    sc.minusOneItem = function minusOneItem(food){
        if(food.num > 1){
            food.num--;
        }
    }

    sc.removeOrderItemFromShoppingCart = function removeOrderItemFromShoppingCart(foodId){
        
    }

    sc.showShoppingCart = function showShoppingCart(){
        console.log("show.........");
        // alert("show");

    }


    
}

module.exports = shoppingCartController;